home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / gfx / show / gs_src_gs.lha / gs5.03 / ialloc.c < prev    next >
C/C++ Source or Header  |  1996-10-26  |  8KB  |  260 lines

  1. /* Copyright (C) 1993, 1995, 1996 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* ialloc.c */
  20. /* Memory allocator for Ghostscript interpreter */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "errors.h"
  24. #include "gsstruct.h"
  25. #include "gxarith.h"            /* for small_exact_log2 */
  26. #include "iref.h"            /* must precede iastate.h */
  27. #include "iastate.h"
  28. #include "ivmspace.h"
  29. #include "store.h"
  30.  
  31. /*
  32.  * Define global and local instances.
  33.  */
  34. gs_dual_memory_t gs_imemory;
  35.  
  36. /* Imported from gsalloc.c */
  37. gs_ref_memory_t *ialloc_alloc_state(P2(gs_memory_t *, uint));
  38.  
  39. #define imem ((gs_ref_memory_t *)mem)
  40.  
  41. /* Initialize the allocator */
  42. void
  43. ialloc_init(gs_memory_t *mem, uint chunk_size, bool level2)
  44. {    gs_ref_memory_t *ilmem = ialloc_alloc_state(mem, chunk_size);
  45.     gs_ref_memory_t *igmem =
  46.         (level2 ?
  47.          ialloc_alloc_state(mem, chunk_size) :
  48.          ilmem);
  49.     gs_ref_memory_t *ismem = ialloc_alloc_state(mem, chunk_size);
  50.     int i;
  51.  
  52.     for ( i = 0; i < countof(gs_imemory.spaces.indexed); i++ )
  53.       gs_imemory.spaces.indexed[i] = 0;
  54.     gs_imemory.space_local = ilmem;
  55.     gs_imemory.space_global = igmem;
  56.     gs_imemory.space_system = ismem;
  57.     gs_imemory.reclaim = 0;
  58.     /* Level 1 systems have only local VM. */
  59.     igmem->space = avm_global;
  60.     ilmem->space = avm_local;    /* overrides if ilmem == igmem */
  61.     igmem->global = ilmem->global = igmem;
  62.     ismem->space = avm_system;
  63.     ialloc_set_space(&gs_imemory, avm_global);
  64. }
  65.  
  66. /* ================ Local/global VM ================ */
  67.  
  68. /* Get the space attribute of an allocator */
  69. uint
  70. imemory_space(gs_ref_memory_t *iimem)
  71. {    return iimem->space;
  72. }
  73.  
  74. /* Select the allocation space. */
  75. void
  76. ialloc_set_space(gs_dual_memory_t *dmem, uint space)
  77. {    gs_ref_memory_t *mem = dmem->spaces.indexed[space >> r_space_shift];
  78.     dmem->current = mem;
  79.     dmem->current_space = mem->space;
  80. }
  81.  
  82. /* Reset the requests. */
  83. void
  84. ialloc_reset_requested(gs_dual_memory_t *dmem)
  85. {    dmem->space_system->gc_status.requested = 0;
  86.     dmem->space_global->gc_status.requested = 0;
  87.     dmem->space_local->gc_status.requested = 0;
  88. }
  89.  
  90. /* ================ Refs ================ */
  91.  
  92. /*
  93.  * As noted in iastate.h, every run of refs has an extra ref at the end
  94.  * to hold relocation information for the garbage collector;
  95.  * since sizeof(ref) % obj_align_mod == 0, we never need to
  96.  * allocate any additional padding space at the end of the block.
  97.  */
  98.         
  99. /* Allocate an array of refs. */
  100. int
  101. gs_alloc_ref_array(gs_ref_memory_t *mem, ref *parr, uint attrs,
  102.   uint num_refs, client_name_t cname)
  103. {    ref *obj;
  104.     /* If we're allocating a run of refs already, */
  105.     /* and we aren't about to overflow the maximum run length, use it. */
  106.     if ( mem->cc.rtop == mem->cc.cbot &&
  107.          num_refs < (mem->cc.ctop - mem->cc.cbot) / sizeof(ref) &&
  108.          mem->cc.rtop - (byte *)mem->cc.rcur + num_refs * sizeof(ref) <
  109.            max_size_st_refs
  110.        )
  111.       {    obj = (ref *)mem->cc.rtop - 1;    /* back up over last ref */
  112.         if_debug4('A', "[a%d:+$ ]%s(%u) = 0x%lx\n", imem->space,
  113.               client_name_string(cname), num_refs, (ulong)obj);
  114.         mem->cc.rcur[-1].o_size += num_refs * sizeof(ref);
  115.           {    ref *end = (ref *)(mem->cc.rtop = mem->cc.cbot +=
  116.                          num_refs * sizeof(ref));
  117.             make_mark(end - 1);
  118.           }
  119.       }
  120.     else
  121.       {    /*
  122.          * Allocate a new run.  We have to distinguish 3 cases:
  123.          *    - Same chunk: pcc unchanged, end == cc.cbot.
  124.          *    - Large chunk: pcc unchanged, end != cc.cbot.
  125.          *    - New chunk: pcc changed.
  126.          */
  127.         chunk_t *pcc = mem->pcc;
  128.         ref *end;
  129.         obj = gs_alloc_struct_array((gs_memory_t *)mem, num_refs + 1,
  130.                         ref, &st_refs, cname);
  131.         if ( obj == 0 )
  132.           return_error(e_VMerror);
  133.         /* Set the terminating ref now. */
  134.         end = (ref *)obj + num_refs;
  135.         make_mark(end);
  136.         /* Set has_refs in the chunk. */
  137.         if ( mem->pcc != pcc || mem->cc.cbot == (byte *)(end + 1) )
  138.           {    /* Ordinary chunk. */
  139.             mem->cc.rcur = (obj_header_t *)obj;
  140.             mem->cc.rtop = (byte *)(end + 1);
  141.             mem->cc.has_refs = true;
  142.           }
  143.         else
  144.           {    /* Large chunk. */
  145.             /* This happens only for very large arrays, */
  146.             /* so it doesn't need to be cheap. */
  147.             chunk_locator_t cl;
  148.             cl.memory = mem;
  149.             cl.cp = mem->clast;
  150.             chunk_locate_ptr(obj, &cl);
  151.             cl.cp->has_refs = true;
  152.           }
  153.       }
  154.     make_array(parr, attrs | mem->space, num_refs, obj);
  155.     return 0;
  156. }
  157.  
  158. /* Resize an array of refs.  Currently this is only implemented */
  159. /* for shrinking, not for growing. */
  160. int
  161. gs_resize_ref_array(gs_ref_memory_t *mem, ref *parr,
  162.   uint new_num_refs, client_name_t cname)
  163. {    uint old_num_refs = r_size(parr);
  164.     uint diff;
  165.     ref *obj = parr->value.refs;
  166.     if ( new_num_refs > old_num_refs || !r_has_type(parr, t_array) )
  167.       return_error(e_Fatal);
  168.     diff = old_num_refs - new_num_refs;
  169.     /* Check for LIFO.  See gs_free_ref_array for more details. */
  170.     if ( mem->cc.rtop == mem->cc.cbot &&
  171.          (byte *)(obj + (old_num_refs + 1)) == mem->cc.rtop
  172.        )
  173.       {    /* Shorten the refs object. */
  174.         ref *end = (ref *)(mem->cc.cbot = mem->cc.rtop -=
  175.                      diff * sizeof(ref));
  176.         if_debug4('A', "[a%d:<$ ]%s(%u) 0x%lx\n", imem->space,
  177.               client_name_string(cname), diff, (ulong)obj);
  178.         mem->cc.rcur[-1].o_size -= diff * sizeof(ref);
  179.         make_mark(end - 1);
  180.       }
  181.     else
  182.       {    /* Punt. */
  183.         if_debug4('A', "[a%d:<$#]%s(%u) 0x%lx\n", imem->space,
  184.               client_name_string(cname), diff, (ulong)obj);
  185.         imem->lost.refs += diff * sizeof(ref);
  186.       }
  187.     r_set_size(parr, new_num_refs);
  188.     return 0;
  189. }
  190.  
  191. /* Deallocate an array of refs.  Only do this if LIFO, or if */
  192. /* the array occupies an entire chunk by itself. */
  193. void
  194. gs_free_ref_array(gs_ref_memory_t *mem, ref *parr, client_name_t cname)
  195. {    uint num_refs = r_size(parr);
  196.     ref *obj = parr->value.refs;
  197.     /*
  198.      * Compute the storage size of the array, and check for LIFO
  199.      * freeing or a separate chunk.  Note that the array might be packed;
  200.      * for the moment, if it's anything but a t_array, punt.
  201.      * The +1s are for the extra ref for the GC.
  202.      */
  203.     if ( !r_has_type(parr, t_array) )
  204.       ;        /* don't look for special cases */
  205.     else if ( mem->cc.rtop == mem->cc.cbot &&
  206.           (byte *)(obj + (num_refs + 1)) == mem->cc.rtop
  207.         )
  208.       {    if ( (obj_header_t *)obj == mem->cc.rcur )
  209.           {    /* Deallocate the entire refs object. */
  210.             gs_free_object((gs_memory_t *)mem, obj, cname);
  211.             mem->cc.rcur = 0;
  212.             mem->cc.rtop = 0;
  213.           }
  214.         else
  215.           {    /* Deallocate it at the end of the refs object. */
  216.             if_debug4('A', "[a%d:-$ ]%s(%u) 0x%lx\n",
  217.                   imem->space, client_name_string(cname),
  218.                   num_refs, (ulong)obj);
  219.             mem->cc.rcur[-1].o_size -= num_refs * sizeof(ref);
  220.             mem->cc.rtop = mem->cc.cbot = (byte *)(obj + 1);
  221.             make_mark(obj);
  222.           }
  223.         return;
  224.       }
  225.     else if ( num_refs >= (mem->large_size / arch_sizeof_ref - 1) )
  226.       {    /* See if this array has a chunk all to itself. */
  227.         /* We only make this check when freeing very large objects, */
  228.         /* so it doesn't need to be cheap. */
  229.         chunk_locator_t cl;
  230.         cl.memory = imem;
  231.         cl.cp = imem->clast;
  232.         if ( chunk_locate_ptr(obj, &cl) &&
  233.              obj == (ref *)((obj_header_t *)(cl.cp->cbase) + 1) &&
  234.              (byte *)(obj + (num_refs + 1)) == cl.cp->cend
  235.            )
  236.           {    /* Free the chunk. */
  237.             if_debug4('a', "[a%d:-$L]%s(%u) 0x%lx\n",
  238.                   imem->space, client_name_string(cname),
  239.                   num_refs, (ulong)obj);
  240.             alloc_free_chunk(cl.cp, imem);
  241.             return;
  242.           }
  243.       }
  244.     /* Punt. */
  245.     if_debug4('A', "[a%d:-$#]%s(%u) 0x%lx\n", imem->space,
  246.           client_name_string(cname), num_refs, (ulong)obj);
  247.     imem->lost.refs += num_refs * sizeof(ref);
  248. }
  249.  
  250. /* Allocate a string ref. */
  251. int
  252. gs_alloc_string_ref(gs_ref_memory_t *mem, ref *psref,
  253.   uint attrs, uint nbytes, client_name_t cname)
  254. {    byte *str = gs_alloc_string((gs_memory_t *)mem, nbytes, cname);
  255.     if ( str == 0 )
  256.       return_error(e_VMerror);
  257.     make_string(psref, attrs | mem->space, nbytes, str);
  258.     return 0;
  259. }
  260.